import { createVNode, render } from 'vue'

import XtxConfirm from './xtx-confirm'

export default ({ title, content }) => {
  // 在body上添加一个div
  const div = document.createElement('div')
  div.setAttribute('class', 'xtx-confirm-container')
  document.body.append(div)

  return new Promise((resolve, reject) => {
    // 取消的回调
    const cancelCallback = () => {
      render(null, div)
      reject(new Error('取消了'))
    }

    // 确定的回调
    const okCallback = () => {
      render(null, div)
      resolve()
    }

    const vNode = createVNode(XtxConfirm, {
      title,
      content,
      okCallback,
      cancelCallback
    })

    render(vNode, div)
  })
}
